package org.bsplus.client.mvp;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;
import org.bsplus.client.event.InitListIssueEvent;
import org.bsplus.client.event.InitListIssueEventHandler;
import org.bsplus.client.event.SetCentreComponentEvent;
import org.bsplus.client.util.GenericAsyncCallback;
import org.bsplus.rpc.IssueRPCService;
import org.bsplus.rpc.IssueRPCServiceAsync;
import org.bsplus.rpc.dto.IssueListItemDTO;
import org.bsplus.util.pagination.PageLoadRequest;
import org.bsplus.util.pagination.ResultPage;

public class ListIssuePresenter extends WidgetPresenter<ListIssuePresenter.Display> implements InitListIssueEventHandler
{
    private IssueRPCServiceAsync issueRPCService = GWT.create(IssueRPCService.class);
   
    public interface Display extends WidgetDisplay
    {
        void init(RpcProxy<PagingLoadResult<IssueListItemDTO>> proxy);
    }

    @Inject
    public ListIssuePresenter(Display display, EventBus eventBus)
    {
            super(display, eventBus);
            eventBus.addHandler(InitListIssueEvent.TYPE, this);
    }


    @Override
    protected void onBind()
    {
        RpcProxy<PagingLoadResult<IssueListItemDTO>> proxy = new RpcProxy<PagingLoadResult<IssueListItemDTO>>() {

            @Override
            protected void load(Object loadConfig, final AsyncCallback<PagingLoadResult<IssueListItemDTO>> callback)
            {
                final PagingLoadConfig pagingLoadConfig = (PagingLoadConfig) loadConfig;

                issueRPCService.listIssues(new PageLoadRequest(pagingLoadConfig.getLimit(), pagingLoadConfig.getOffset()), new GenericAsyncCallback<ResultPage<IssueListItemDTO>>() {

                    @Override
                    public void onSuccess(ResultPage<IssueListItemDTO> result)
                    {
                        callback.onSuccess(new BasePagingLoadResult<IssueListItemDTO>(result.getResults(), pagingLoadConfig.getOffset(), result.getTotal()));
                    }
                });
            }
        };

        getDisplay().init(proxy);
    }

    @Override
    protected void onUnbind()
    {
    }

    @Override
    public Place getPlace()
    {
        return null;
    }

    @Override
    protected void onPlaceRequest(PlaceRequest request)
    {
    }

    @Override
    public void refreshDisplay()
    {
    }

    @Override
    public void revealDisplay()
    {
    }

    @Override
    public void init(InitListIssueEvent event)
    {
        bind();
        eventBus.fireEvent(new SetCentreComponentEvent(getDisplay().asWidget()));
    }
}
